﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using BlobTowerDefense.Game_Entities;
using Microsoft.Xna.Framework.Graphics;
using BlobTowerDefense.Tools;

namespace BlobTowerDefense.Managers
{
    public struct BulletStats
    {
        public TowerStats towerStats;
        public float BulletSpeed;
        public string PathToModel;

        public BulletStats(TOWERTYPE towerType, float bulletSpeed, string pathToModel)
        {
            this.towerStats = TowerManager.DefaultTowerStats[towerType];
            this.BulletSpeed = bulletSpeed;
            this.PathToModel = pathToModel;
        }
    }

    public class BulletManager
    {
        public static Dictionary<TOWERSPECIAL, BulletStats> DefaultBullets = new Dictionary<TOWERSPECIAL, BulletStats>(6)
        {
            { TOWERSPECIAL.none, new BulletStats(TOWERTYPE.tower1, 10.0f, "Models/Bullets/bullet_standard") },
            { TOWERSPECIAL.fast, new BulletStats(TOWERTYPE.tower2, 10.0f, "Models/Bullets/bullet_fast") },
            { TOWERSPECIAL.ice, new BulletStats(TOWERTYPE.tower3, 10.0f, "Models/Bullets/bullet_ice") },
            { TOWERSPECIAL.poison, new BulletStats(TOWERTYPE.tower4, 10.0f, "Models/Bullets/bullet_poison") },
            { TOWERSPECIAL.strong, new BulletStats(TOWERTYPE.tower5, 10.0f, "Models/Bullets/bullet_strong") },
            { TOWERSPECIAL.range, new BulletStats(TOWERTYPE.tower6, 10.0f, "Models/Bullets/bullet_sniper") }
        };

        public List<Bullet> Bullets;

        public BulletManager()
        {
            Bullets = new List<Bullet>();
        }

        public void UpdateBullets()
        {
            if ((Bullets != null))
            {
                for (int i = 0; i < Bullets.Count; i++)
                {
                    if (!Bullets[i].Exists)
                    {
                        Bullets.Remove(Bullets[i]);                        
                    }
                    else
                    {
                        Bullets[i].Update();
                    }
                }
            }
        }

        public bool CheckEnemyShot(EnemyEntity enemy)
        {
            foreach (Bullet bullet in Bullets)
            {
                foreach (ModelMesh meshBullet in bullet.Model.Meshes)
                {
                    BoundingSphere boundingSphere_Bullet = BoundingSphere_Tools.TransformBoundingSphere(meshBullet.BoundingSphere, bullet.Position, bullet.Scale);

                    foreach (ModelMesh meshEnemy in enemy.Model.Meshes)
                    {
                        BoundingSphere boundingSphere_Enemy = BoundingSphere_Tools.TransformBoundingSphere(meshEnemy.BoundingSphere, enemy.Position, enemy.Scale);

                        if (boundingSphere_Enemy.Intersects(boundingSphere_Bullet))
                        {
                            enemy.Hit(bullet);
                            bullet.Exists = false;                            
                            return true;
                        }
                    }
                }
            }

            return false;
        }

        public void DrawBullets()
        {
            foreach (Bullet bullet in Bullets)
            {
                bullet.Draw();
            }
        }

        public void ShootBullet(Vector3 positionStart, Vector3 positionEnemy, TOWERSPECIAL bulletSpecial)
        {
            Vector3 vector = new Vector3(positionEnemy.X - positionStart.X,
                                         positionEnemy.Y - positionStart.Y,
                                         positionEnemy.Z - positionStart.Z);

            vector = new Vector3(vector.X * 2, vector.Y * 2, vector.Z);

            Bullets.Add(new Bullet(positionStart, positionEnemy, DefaultBullets[bulletSpecial]));
        }
    }
}
